DDCTF re1 writeup

The challenge download is a Windows x86 executable named reverse1_final. My first attempt to decompile it in IDA stops with this message:

UKpqB2OeJIZb3vQ.png

That looks like a packed executable, so I checked its PE details with PEiD:

hKvQlNxSXUkTfit.png

The entry point lands in UPX1, which points to UPX. I unpacked the file; the result would no longer run normally, but IDA could at least disassemble it. I found main and renamed the variables to make the flow easier to read:

1MFEDnXg9UsNBxC.png

The input lands in str_in, and the result of a series of operations ends up in v4. Since that value is compared with DDCTF{reverseME}, the transformation is the function I need to understand:

VouKMtpCzDe5qvJ.png

Inside, the function reads the input and looks up values in data. That looks like a substitution table, although its contents are not immediately apparent from the decompilation.

I clicked through to data and found this:

2RWCdanXDklS6yN.png

No obvious table yet. A little farther down, though, there is a promising string:

mAbZtyvi7hp4U9W.png

It looks enough like the alphabet to test. I wrote a small program to find the index of each character in the target string:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <string.h>
int main()
{
char key[] = {"DDCTF{reverseME}"};
char alpha[] = {"~}|{zyxwvutsrqponmlkjihgfedcba`_^]\\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)(,&%$#\"!"} ;
for (int i=0;i<strlen(key);i++){
for(int j=0;j<strlen(alpha);j++){
if(key[i]==alpha[j]) printf("%d ",j);
}
}
return 0;
}

It prints 58 58 59 42 56 3 12 25 8 25 12 11 25 49 57 1.

Those indices do not look like something I can enter as the answer. I turned to OllyDbg to see how the lookup actually works, but the address was randomized, making the breakpoint awkward to set. PEtools let me disable address-space randomization:

ARPnsoW9KLTF6bC.png

J6nktLVENRfDP8S.png

After saving the change, I set a breakpoint at sub_401000 and supplied ABCDEFG as a test input:

T8p92g5ijaJnEVP.png

1
2
3
4
5
6
7
8
loc_401020:
movsx eax, byte ptr [ebx+ecx]
mov dl, ds:data[eax]
mov eax, ebp
mov [ecx], dl
inc edi
inc ecx
lea esi, [eax+1]

At this point, the code reads data[eax]; eax comes from the byte addressed by ebx+ecx. The register values show the lookup in action:

uOlmBJwN5yzW1VM.png

The test shows A mapping to ]. I adjusted the C program to account for that offset:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <string.h>
int main()
{
char key[] = {"ABCDEFG"};
char alpha[] = {"~}|{zyxwvutsrqponmlkjihgfedcba`_^],[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)(,&%$#\"!"} ;
for (int i=0;i<strlen(key);i++){
for(int j=0;j<strlen(alpha);j++){
if(key[i]==alpha[j]) printf("%c ",j-'='+']');
}
}
return 0;
}

The adjustment is -'='+']'. OllyDbg confirms that the test maps to ] \ [ Z Y X W, so I replaced the test key with DDCTF{reverseME} and checked the resulting input:

fHJZqe3P9TbuYW2.png

yjVghaFuMRcZtYG.png

LkRNiHTtzajIAX8.png

That does it—the program reveals the flag!

Celebration! ★,°:.☆( ̄▽ ̄)/$:.°★ 。